package com.cs4590.finalProject;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.os.Environment;
import android.util.Log;

import com.db4o.Db4oEmbedded;
import com.db4o.ObjectContainer;
import com.db4o.query.Predicate;

/**
 * ObjectDatabase 
 * This class provides an interface to connecting with the db4o
 * object database. The only class that interacts with this one 
 * is the WFSettingsManager.
 *
 *@author David Murray
 */
public class ObjectDatabase {

	//--------------------------------------------------------------------------------
	private ObjectContainer oc;
	private Context context;

	//--------------------------------------------------------------------------------
	public ObjectDatabase(Context c) {
		this.context = c;
		open();
	}

	//--------------------------------------------------------------------------------
	public void open() {
		try {
			oc = Db4oEmbedded.openFile(context.getDir("data", 0) + "/" + "databaseFile.db4o");
		} 
		catch (Exception e) {
			Log.e("ObjectDatabase: open", e.toString());
		}
	}

	//--------------------------------------------------------------------------------
	public void close() {
		try {
			oc.close();
		}
		catch(Exception e) {
			Log.e("ObjectDatabase: close", e.toString());
		}
	}

	//--------------------------------------------------------------------------------
	//--------------------------------------------------------------------------------
	//--------/ WFUserInfo methods
	//--------------------------------------------------------------------------------
	public void store(String str) {
		try {
			if(!exists(str) && !str.equals("")) {
				oc.store(new SoundFile(str));
			}
//		    oc.store(str);
		    oc.commit();
		}
		catch(Exception e) {
			Log.e("ObjectDatabase: store", "Problem saving " + str.toString() + ": " + e.toString());
		}
	}

	//--------------------------------------------------------------------------------
	public void delete(String str) {
		try {
		    List<SoundFile> strings = oc.query(new StringFinder(str));
		    SoundFile aString = strings.get(0);
		    for(SoundFile string : strings) {
		    	aString = string;
		    }
		     
		    oc.delete(aString);
		    File f = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + str + ".mp3");
		    f.delete();
		    oc.commit();
		}
		catch(Exception e) {
			Log.e("ObjectDatabase: deleteWFUserInfo", "Problem deleting " + str.toString() + ": " + e.toString());
		}
	}
	
	//--------------------------------------------------------------------------------
	public ArrayList<String> getAll() {
		ArrayList<String> retVal2 = new ArrayList<String>();
		List<SoundFile> retVal = new ArrayList<SoundFile>();
		try {
			retVal = oc.query(SoundFile.class);
			for(SoundFile x : retVal) {
				retVal2.add(x.getName());
			}
		} 
		catch(Exception e) {
			Log.e("ObjectDatabase: getAll", e.toString());
		}
		return retVal2;
	}

	//--------------------------------------------------------------------------------
	public class StringFinder extends Predicate<SoundFile> {
		/**
		 * 
		 */
		private static final long serialVersionUID = 8253690774005437149L;
		
		String str;
		
		public StringFinder(String str) {
			this.str = str;
		}
		
		public boolean match(SoundFile s) {
			return (s.getName()).equals(str);
		}
	}
	
	private boolean exists(String str) {
		ArrayList<String> x = this.getAll();
		if(x.contains(str)) {
			return true;
		} else {
			return false;
		}
	}
}
